<?php
/**
 * Created by PhpStorm.
 * User: guanfangge
 * Date: 4/26/22
 * Time: 10:51 AM
 */
namespace App\HttpController\Admin;

use App\Models\User;
use EasySwoole\Http\AbstractInterface\Controller;


class Test extends Controller
{

    public function test()
    {
        $user = new User();
        $result = $user->all();
        return $this->writeJson(200,$result,'请求成功');
    }

    public function  add(){
        $user=new User();
        $user->setAttr("name","hsl");
        $user->setAttr("phone","18334111890");
        $id=$user->save();
        return $this->writeJson(200,$id,'新增成功');
    }

    public function  del(){
        $user = User::create()->get(1);
        $result=$user->destroy();
        return $this->writeJson(200,$result);
    }

    public function  update(){
        $user = User::create()->get(19);
//获取后指定字段赋值
        $user->name = "Tom";
        $result=$user->update();
        return $this->writeJson(200,$result);
    }

    public function  many(){

        $data=User::create()->with('subUser')
            ->all();
        return $this->writeJson(200,$data);
    }

    /**
     * 接口请求量
     *
     * CreateTime: 2020/8/19 12:37 上午
     */
    public function requestTotal()
    {
        $this->response()->write('请求数+1' . PHP_EOL);

        // 还可以 return，但不要两个方法互相调用，会导致死循环
    }

    /**
     * 此控制器抛异常时会执行此方法
     *
     * @param \Throwable $throwable
     * @throws \Throwable
     * CreateTime: 2020/8/19 12:48 上午
     */
    public function onException(\Throwable $throwable): void
    {
        parent::onException($throwable); // TODO: Change the autogenerated stub
    }

    /**
     * gc 方法将在执行完 afterAction 方法之后自动调用，可自行覆盖实现其他的 gc 回收逻辑
     *
     * CreateTime: 2020/8/19 12:52 上午
     */
    public function gc()
    {
        parent::gc(); // TODO: Change the autogenerated stub
    }

    /**
     * 当控制器方法执行结束之后将调用该方法，可自行覆盖该方法实现数据回收等逻辑
     *
     * @param string|null $actionName
     * CreateTime: 2020/8/19 12:51 上午
     */
    public function afterAction(?string $actionName): void
    {
        parent::afterAction($actionName); // TODO: Change the autogenerated stub
    }

    /**
     * 当请求方法未找到时，自动调用该方法，可自行覆盖该方法实现自己的逻辑
     *
     * @param string|null $action
     * CreateTime: 2020/8/19 12:51 上午
     */
    public function actionNotFound(?string $action)
    {
        parent::actionNotFound($action); // TODO: Change the autogenerated stub
    }

    /**
     * 所有控制器请求都会先经过该方法，如果此方法返回 false 则请求不继续往下执行，可用于权限验证
     *
     * @param string|null $action
     * @return bool|null
     * CreateTime: 2020/8/19 12:52 上午
     */
    public function onRequest(?string $action): ?bool
    {
        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }
}